﻿using System.Linq;
using SeaBattle.Domain.Abstract;
using SeaBattle.Domain.Abstract.Services;
using SeaBattle.Domain.Entities;
using SeaBattle.Domain.Factory;

namespace SeaBattle.Domain.Concrete.Services
{
    public class PlayerService : IPlayerService
    {
        private readonly IPlayerRepository _playerRepository;

        public PlayerService()
        {
            if (_playerRepository == null)
            {
                _playerRepository = PlayerFactory.PlayerRepository;
            }
        }

        public bool IsPlayerExists(string login)
        {
            return _playerRepository.Players.FirstOrDefault(x => x.Login == login) != null;
        }

        public void RegisterNewPlayer(Player player)
        {
            _playerRepository.SavePlayer(player);
        }

        public Player GetPlayer(string login)
        {
            return _playerRepository.Players.Single(x => x.Login == login);
        }

        public decimal GetPlayerBalance(string login)
        {
            return GetPlayer(login).Balance;
        }
    }
}
